<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<link type="text/css" rel="stylesheet" href="../build/logger/assets/logger.css" />
	<link type="text/css" rel="stylesheet" href="../build/yuitest/assets/testlogger.css" />

	<script type="text/javascript" src="../build/yahoo-dom-event/yahoo-dom-event.js"></script>
	<script type="text/javascript" src="../build/logger/logger-min.js"></script>
	<script type="text/javascript" src="../build/yuitest/yuitest-beta-min.js"></script>
<!-- 	<script type="text/javascript" src="../build/container/container_core-min.js"></script> -->
	<script type="text/javascript" src="http://competitor.corp.yahoo.com/presentation/2.x/widget/container/src/js/Config.js"></script>
</head>
<body>
    <script type="text/javascript">
	(function() { 

		YAHOO.namespace("tests");

		var	TestCase = YAHOO.tool.TestCase,
			Assert=YAHOO.util.Assert,
			ObjectAssert=YAHOO.util.ObjectAssert,
			ArrayAssert=YAHOO.util.ArrayAssert;

		var logger;

		YAHOO.tests.ConfigOwner = function(test) {
			this.id = "ConfigOwner";
	   		this.cfg = new YAHOO.util.Config(this);
			
			this.events = [];
			
			this.clearEventLog = function() {
				this.events = [];
			};
			
			this.getFiredEvents = function() {
				var handlers = [];
				for (var i = 0; i < this.events.length; i++) {
					handlers.push(this.events[i].type);
				}
				return handlers;
			};
	   	};

		var configTemplate = {

			name: "configtestcase",

			setUp : function() {
				this.cfgOwner = new YAHOO.tests.ConfigOwner();

				var h = this.genericCfgHandler;
				var o = this.cfgOwner;

		    	o.cfg.addProperty("sONe", 	{value:"str1", handler:h, suppressEvent:true });
		    	o.cfg.addProperty("sTwO", 	{value:"str2", handler:h, suppressEvent:false });
		    	o.cfg.addProperty("sThree", {value:"str3", handler:h, supercedes:["STWO", "sone"] });

		    	o.cfg.addProperty("nOnE", 	{value:777, handler:h, validator:o.cfg.checkNumber });
		    	o.cfg.addProperty("nTwo", 	{value:"Fourteen", handler:h, validator:o.cfg.checkNumber });

		    	o.cfg.addProperty("bone", 	{value:false, handler:h, validator:o.cfg.checkBoolean });
		    	o.cfg.addProperty("bTWo", 	{value:true, handler:h, validator:o.cfg.checkBoolean });
		    	o.cfg.addProperty("BTHREE", {value:"CompletelyUnfounded", handler:h, validator:o.cfg.checkBoolean });

				o.cfg.fireQueue();
			},

			tearDown : function() {
				this.cfgOwner.cfg.destroy();
				this.cfgOwner.cfg = null;
				this.cfgOwner = null;
			},

			genericCfgHandler : function(type, args, obj) {
				this.events.push({type:type, args:args, obj:obj});
			},

			customCfgHandler : function(type, args, obj) {
				this.events.push({type:type+"_custom", args:args, obj:obj});
			},

			testAddProperty : function() {
				var o = this.cfgOwner;

				var expectedHandlers = ["sthree", "stwo", "none", "bone", "btwo"];
				var actualHandlers = o.getFiredEvents();
				ArrayAssert.itemsAreEqual(expectedHandlers, actualHandlers);
			},

			testGetConfig : function() {
				// TODO
			},

			testGetProperty : function() {
				var o = this.cfgOwner;
				o.clearEventLog();

		    	Assert.areEqual("str1", o.cfg.getProperty("SOne"));
		    	Assert.areEqual("str2", o.cfg.getProperty("STWO"));
		    	Assert.areEqual("str3", o.cfg.getProperty("sThreE"));
			},

			testQueueOrderAndValidation : function() {
				var o = this.cfgOwner;
				o.clearEventLog();

				o.cfg.queueProperty("bTWO", "NotBoolean");
				o.cfg.queueProperty("SonE", "str11");
				o.cfg.queueProperty("StWo", "str22");
				o.cfg.queueProperty("None", 20);
				o.cfg.queueProperty("StHree", "str33"); // Should sthree's supercede ordering really change the order of sONE, sTWO?

				o.cfg.fireQueue();

				var expectedHandlers = ["none", "sthree", "stwo", "sone"];
				var actualHandlers = o.getFiredEvents();
				ArrayAssert.itemsAreEqual(expectedHandlers, actualHandlers);
			},

			testRefireEvent : function() {
				var o = this.cfgOwner;
				o.clearEventLog();

		    	o.cfg.refireEvent("STwo");
		    	o.cfg.refireEvent("Sone");
		    	o.cfg.refireEvent("NTwo"); // Value is invalid - should it really be firing?

				var expectedHandlers = ["stwo", "sone", "ntwo"];
				var actualHandlers = o.getFiredEvents();
				ArrayAssert.itemsAreEqual(expectedHandlers, actualHandlers);
			},

			testRefresh : function() {
				var o = this.cfgOwner;
				o.clearEventLog();

				o.cfg.refresh();

				var expectedHandlers = ["sone", "stwo", "sthree", "none", "ntwo", "bone", "btwo", "bthree"];
				var actualHandlers = o.getFiredEvents();
				ArrayAssert.itemsAreEqual(expectedHandlers, actualHandlers);
			},

			testResetProperty : function() {
				var o = this.cfgOwner;
				o.clearEventLog();

				// All properties should be set to values specified by ApplyConfig
				o.cfg.applyConfig({
					sOnE:"str1111",
					sTwo: "str2222",
					Sthree: "str3333",
					nONE:1111,
					nTWo:2222, 
					bONE:true, 
					btwo:true, 
					bTHREE:true
				}, true);


				o.cfg.setProperty("sonE", "str11");
				o.cfg.setProperty("sTWO", "str22");
				o.cfg.setProperty("sTHREE", "str33");
				o.cfg.setProperty("nONE", 11);
				o.cfg.setProperty("nTWO", 22);
				o.cfg.setProperty("BOnE", false);
				o.cfg.setProperty("BTwO", false);
				o.cfg.setProperty("BthreE", false);																												


                o.cfg.resetProperty("soNe");
                o.cfg.resetProperty("stWo");
				o.cfg.resetProperty("stHree");
				o.cfg.resetProperty("nOnE");
				o.cfg.resetProperty("nTwO");
				o.cfg.resetProperty("bONE");
				o.cfg.resetProperty("bTWO");
				o.cfg.resetProperty("BTHREE");

		
				// Should values be reset to defaults and not applyConfig values
				Assert.areEqual("str1111", o.cfg.getProperty("sONE"));
				Assert.areEqual("str2222", o.cfg.getProperty("stWo"));
				Assert.areEqual("str3333", o.cfg.getProperty("sTHREE"));
				Assert.areEqual(1111, o.cfg.getProperty("NONE"));
				Assert.areEqual(2222, o.cfg.getProperty("NtWO"));
				Assert.areEqual(true, o.cfg.getProperty("bONE"));
				Assert.areEqual(true, o.cfg.getProperty("BtWO"));																								
				Assert.areEqual(true, o.cfg.getProperty("BThREE"));				
			},

			testSubscribeToConfigEvent : function() {
				var o = this.cfgOwner;
				o.clearEventLog();

				o.cfg.subscribeToConfigEvent("sOne", this.customCfgHandler);
				o.cfg.setProperty("SOne", "str11111");

				var expected = ["sone", "sone_custom"];
				var actual = o.getFiredEvents();

				ArrayAssert.itemsAreEqual(expected, actual);

				o.clearEventLog();

				o.cfg.unsubscribeFromConfigEvent("sone", this.customCfgHandler);
				o.cfg.setProperty("sONe", "str11111");
				
				expected = ["sone"];
				actual = o.getFiredEvents();

				ArrayAssert.itemsAreEqual(expected, actual);

			}
		}

		var configTestCase = new YAHOO.tool.TestCase(configTemplate);

        function runTests() {

			var logger = new YAHOO.tool.TestLogger();
			var testSuite=new YAHOO.tool.TestSuite("configsuite");
			testSuite.add(configTestCase);

			logger.clearConsole();

			YAHOO.tool.TestRunner.clear();
			YAHOO.tool.TestRunner.add(testSuite);

            if (parent && parent != window) {
                YAHOO.tool.TestManager.load();
            } else {
                YAHOO.tool.TestRunner.run();
            }
        }

		YAHOO.util.Event.addListener(window, "load", runTests);

	})();
    </script>
</body>
</html>
